from Tkinter import *
from tkSimpleDialog import *
from keyexpression import *
import re

class TextualAnalysisFrame(Frame):
    """
    Represents the frame that provides a user interface to access the lexical analysis of the project.
    """
    
    newlineRegex = re.compile(r"\s*\r?\n[\s\r\n]*")
    
    def __init__(self, parentControl, listener):
        Frame.__init__(self, parentControl)
        self.listener = listener
        
        label = Label(self, text="Mandat:")
        label.grid(column = 0, columnspan = 4)
        
        self.mandateTextBox = Text(self, width=50, height=20)
        self.mandateTextBox.config(state = DISABLED)
        self.mandateTextBox.grid(row = 1, column = 0, columnspan = 4)
        
        label = Label(self, text="Noms: ")
        label.grid(row = 2, column = 1)
        
        label = Label(self, text="Verbes: ")
        label.grid(row = 2, column = 2)
        
        label = Label(self, text="Adjectifs: ")
        label.grid(row = 2, column = 3)
        
        label = Label(self, text="Explicite: ")
        label.grid(row = 3, column = 0)
        
        self.explicitNounsTextBox = Text(self, width=15, height=10)
        self.explicitNounsTextBox.grid(row = 3, column = 1)
        
        self.explicitVerbsTextBox = Text(self, width=15, height=10)
        self.explicitVerbsTextBox.grid(row = 3, column = 2)
        
        self.explicitAdjectivesTextBox = Text(self, width=15, height=10)
        self.explicitAdjectivesTextBox.grid(row = 3, column = 3)
        
        label = Label(self, text="Implicite: ")
        label.grid(row = 4, column = 0)
        
        self.implicitNounsTextBox = Text(self, width=15, height=10)
        self.implicitNounsTextBox.grid(row = 4, column = 1)
        
        self.implicitVerbsTextBox = Text(self, width=15, height=10)
        self.implicitVerbsTextBox.grid(row = 4, column = 2)
        
        self.implicitAdjectivesTextBox = Text(self, width=15, height=10)
        self.implicitAdjectivesTextBox.grid(row = 4, column = 3)
        
        button = Button(self, text="Sauvegarder", command=self._onSavePressed)
        button.grid(row = 5, column = 0, columnspan = 4)
    
    def setMandate(self, mandate):
        self.mandateTextBox.config(state = NORMAL)
        self.mandateTextBox.delete(1.0, END)
        self.mandateTextBox.insert(END, mandate)
        self.mandateTextBox.config(state = DISABLED)
        
    def _setTerms(self, textBox, terms):
        textBox.delete(1.0, END)
        textBox.insert(END, "\n".join(terms))
        
    def _addTerm(self, textBox, term):
        if len(textBox.get(1.0, END).strip()) == 0:
            textBox.delete(1.0, END)
            textBox.insert(END, term)
        else:
            textBox.insert(END, "\n" + term)
        
    def clearKeyExpressions(self):
        self.explicitNounsTextBox.delete(1.0, END)
        self.explicitVerbsTextBox.delete(1.0, END)
        self.explicitAdjectivesTextBox.delete(1.0, END)
        self.implicitNounsTextBox.delete(1.0, END)
        self.implicitVerbsTextBox.delete(1.0, END)
        self.implicitAdjectivesTextBox.delete(1.0, END)
        
    def addKeyExpression(self, keyExpression):
        if keyExpression.isExplicit:
            if keyExpression.type == "noun":
                self._addTerm(self.explicitNounsTextBox, keyExpression.expression)
            elif keyExpression.type == "verb":
                self._addTerm(self.explicitVerbsTextBox, keyExpression.expression)
            elif keyExpression.type == "adjective":
                self._addTerm(self.explicitAdjectivesTextBox, keyExpression.expression)
        else:
            if keyExpression.type == "noun":
                self._addTerm(self.implicitNounsTextBox, keyExpression.expression)
            elif keyExpression.type == "verb":
                self._addTerm(self.implicitVerbsTextBox, keyExpression.expression)
            elif keyExpression.type == "adjective":
                self._addTerm(self.implicitAdjectivesTextBox, keyExpression.expression)
        
    def addKeyExpressions(self, keyExpressions):
        for keyExpression in keyExpressions:
            self.addKeyExpression(keyExpression)
        
    def setKeyExpressions(self, keyExpressions):
        self.clearKeyExpressions()
        self.addKeyExpressions(keyExpressions)
        
    def _getTerms(self, textBox):
        text = textBox.get(1.0, END).strip()
        terms = self.newlineRegex.split(text)
        terms = filter(lambda x: len(x.strip()) > 0, terms)
        return terms
    
    def _appendKeyExpressions(self, list, textBox, type, isExplicit):
        terms = self._getTerms(textBox)
        for term in terms:
            list.append(KeyExpression(0, term, type, isExplicit))
    
    def getKeyExpressions(self):
        keyExpressions = []
        self._appendKeyExpressions(keyExpressions, self.explicitNounsTextBox, "noun", True)
        self._appendKeyExpressions(keyExpressions, self.explicitVerbsTextBox, "verb", True)
        self._appendKeyExpressions(keyExpressions, self.explicitAdjectivesTextBox, "adjective", True)
        self._appendKeyExpressions(keyExpressions, self.implicitNounsTextBox, "noun", False)
        self._appendKeyExpressions(keyExpressions, self.implicitVerbsTextBox, "verb", False)
        self._appendKeyExpressions(keyExpressions, self.implicitAdjectivesTextBox, "adjective", False)
        return keyExpressions
        
    def _onSavePressed(self):
        self.listener.onSaveTextualAnalysisChanges()